//
//  GestureManager.m
//  Training-Teacher
//
//  Created by dev on 12-10-27.
//
//

#import "GestureManager.h"
#import "Constants.h"

@implementation GestureManager

+ (void)addSwipeLeftGesture:(UIView*)view delegate:(id<UIGestureRecognizerDelegate>)delegate
{
    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
    swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    [view addGestureRecognizer:swipeLeft];
    view.userInteractionEnabled = YES;

    if (delegate) {
        swipeLeft.delegate = delegate;
    } else {
        swipeLeft.delegate = (id<UIGestureRecognizerDelegate>)view;
    }
}

+ (void)addSwipeRightGesture:(UIView*)view delegate:(id<UIGestureRecognizerDelegate>)delegate
{
    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
    swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
    [view addGestureRecognizer:swipeRight];
    view.userInteractionEnabled = YES;
    
    if (delegate) {
        swipeRight.delegate = delegate;
    } else {
        swipeRight.delegate = (id<UIGestureRecognizerDelegate>)view;
    }
}

+ (void)addSwipeGesture:(UIView*)view delegate:(id<UIGestureRecognizerDelegate>)delegate
{
    [self addSwipeLeftGesture:view delegate:delegate];
    [self addSwipeRightGesture:view delegate:delegate];
}

+ (void)addSwipeGesture:(UIView*)view
{
    [self addSwipeGesture:view delegate:nil];
}

+ (void)handleSwipe:(id)sender
{
    UISwipeGestureRecognizer *gesture = sender;
    int direction = (UISwipeGestureRecognizerDirectionLeft == gesture.direction ? SwipeLeft : SwipeRight);
    
    NSDictionary *userInfo = @{SwipeDirectionKey : [NSNumber numberWithInt:direction]};
    [[NSNotificationCenter defaultCenter] postNotificationName:DidPageSwiped object:self userInfo:userInfo];
}

+ (void)addPinchGesture:(UIView*)view
{
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchImage:)];
    [view addGestureRecognizer:pinch];
    view.userInteractionEnabled = YES;
}

+ (void)pinchImage:(UIPinchGestureRecognizer*)gesture
{
    if (gesture.state == UIGestureRecognizerStateEnded) {
        UIView *targetView = gesture.view;
        
        if (gesture.scale < 1) {
            float zoomScale = 1;
            if (targetView.frame.size.height > 768) {
                zoomScale = 768 /targetView.frame.size.height;
            } else if (targetView.frame.size.width > 1024) {
                zoomScale = 1024 /targetView.frame.size.width;
            }
            
            [UIView animateWithDuration:.5 animations:^{
                targetView.transform = CGAffineTransformMakeScale(zoomScale, zoomScale);
            }];
        } else {
            [UIView animateWithDuration:.5 animations:^{
                targetView.transform = CGAffineTransformIdentity;
            }];
        }
    }
}

+ (void)addFreePanGesture:(UIView*)view
{
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleFreePan:)];
    [view addGestureRecognizer:pan];
}

+ (void)handleFreePan:(UIPanGestureRecognizer*)recognizer{
    CGPoint translation = [recognizer translationInView:recognizer.view];
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, recognizer.view.center.y + translation.y);
    [recognizer setTranslation:CGPointMake(0, 0) inView:recognizer.view];
}

+ (void)addHorizontalPanGesture:(UIView*)view
{
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleHorizontalPan:)];
    [view addGestureRecognizer:pan];
}

+ (void)handleHorizontalPan:(UIPanGestureRecognizer*)recognizer{
    CGPoint translation = [recognizer translationInView:recognizer.view];
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, recognizer.view.center.y);
    [recognizer setTranslation:CGPointMake(0, 0) inView:recognizer.view];
}

@end
